import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class MergeArrays {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int len = scanner.nextInt();
        int num = scanner.nextInt();
        scanner.nextLine();

        List<List<Integer>> arrays = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            String[] inputArray = scanner.nextLine().split(",");
            List<Integer> intArray = new ArrayList<>();
            for (String s : inputArray) {
                intArray.add(Integer.parseInt(s));
            }
            arrays.add(intArray);
        }

        List<Integer> result = new ArrayList<>();
        while (true) {
            boolean allEmpty = true;
            for (List<Integer> array : arrays) {
                if (!array.isEmpty()) {
                    allEmpty = false;
                    int takeCount = Math.min(len, array.size());
                    for (int i = 0; i < takeCount; i++) {
                        result.add(array.remove(0));
                    }
                }
            }
            if (allEmpty) {
                break;
            }
        }

        StringBuilder sb = new StringBuilder();
        for (Integer integer : result) {
            sb.append(integer).append(",");
        }
        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
        }
        System.out.println(sb.toString());
    }
}